package com.xindaoapp.framework;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.xindaoapp.frameworkfragment.CartFragment;
import com.xindaoapp.frameworkfragment.CenterFragment;
import com.xindaoapp.frameworkfragment.CircleFragment;
import com.xindaoapp.frameworkfragment.ClassifyFragment;
import com.xindaoapp.frameworkfragment.HomeFragment;
import com.xindaoapp.frameworkfragment.LeftMenuFragment;
import com.xindaoapp.frameworkfragment.MenuFragment;



/**
 * ============================================================
 * 
 * 版权 : 新道APP (c) 2014
 * 
 * 作者 : 贺奕凯
 * 
 * 版本 : 1.0
 * 
 * 创建日期 : 2014-6-17 上午10:01:46
 * 
 * 描述 : MainActivity
 * 
 * 修订历史 ：
 * 
 * ============================================================
 **/
public class MainActivity extends SlidingFragmentActivity implements
		OnClickListener {

	private FrameLayout layout_content;
	private RadioGroup main_radio;
	private int curIndex = R.id.rb_function;
	private SlidingMenu sm;

	// 初始化Fragement标签 
	private LeftMenuFragment leftMenuFragement;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setBehindContentView(R.layout.left_menu_frame);
		setContentView(R.layout.activity_main);

		// 1 获取滑动菜单的对象
		sm = getSlidingMenu();
		initSlidingMenu(savedInstanceState);
		layout_content = (FrameLayout) findViewById(R.id.layout_content);
		main_radio = (RadioGroup) findViewById(R.id.main_radio);

		main_radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			int index = 0;

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_function:
					index = 0;
					sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE); //打开侧边栏
					break;
				case R.id.rb_news_center:
					index = 1;
					sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE); //关闭侧边栏
					break;
				case R.id.rb_smart_service:
					index = 2;
					sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE); //关闭侧边栏
					break;
				case R.id.rb_gov_affairs:
					index = 3;
					sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE); //关闭侧边栏
					break;
				case R.id.rb_setting:
					index = 4;
					sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE); //关闭侧边栏
					break;
				}
				// fragment进行初始化
				Fragment fragment = (Fragment) fragments.instantiateItem(
						layout_content, index);
				// fragment默认选择第0个元素
				fragments.setPrimaryItem(layout_content, 0, fragment);
				// fragment提交事务更新
				fragments.finishUpdate(layout_content);
			}

		});
		main_radio.check(curIndex);
	}

	FragmentStatePagerAdapter fragments = new FragmentStatePagerAdapter(
			getSupportFragmentManager()) {

		@Override
		public int getCount() {
			return 5;
		}

		@Override
		public Fragment getItem(int arg0) {
			Fragment fragment = null;
			switch (arg0) {
			case 0:
				fragment = new HomeFragment(sm);
				break;
			case 1:
				fragment = new ClassifyFragment(MainActivity.this);
				break;
			case 2:
				fragment = new CircleFragment();
				break;
			case 3:
				fragment = new CartFragment();
				break;
			case 4:
				fragment = new CenterFragment(MainActivity.this,
						getWindowManager());
				break;

			}
			return fragment;
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ib_title_right:

			break;

		default:
			break;
		}
	}

	public void switchFragment(Fragment f) {
		// TODO Auto-generated method stub
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_fragme, f).commit();
		// 自动判断滑动菜单开关
		sm.toggle();
	}

	/**
	 * //初始化SlidingMenu
	 * 
	 * @param savedInstanceState
	 */
	private void initSlidingMenu(Bundle savedInstanceState) {
		sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow); //设置阴影界面
		
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
//		sm.setBehindWidthRes(R.dimen.slidingmenu_offset);
		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);

		sm.setSecondaryMenu(R.layout.left_drawer_fragment);
//		sm.setSecondaryMenu(R.layout.left_menu_frame);
		leftMenuFragement = new LeftMenuFragment();
		@SuppressWarnings("unused")
		MenuFragment menu =  new MenuFragment();
		getSupportFragmentManager().beginTransaction()
		.replace(R.id.left_menu_frame, leftMenuFragement, "LeftMenu").commit();

		sm.setMode(SlidingMenu.LEFT);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}
	
	

}

